import { computed, ref, Ref, watchEffect } from 'vue'
import { TotalData, NumData, Level, Num } from '../types'
import { useLevelDictValueData } from './useLevelDictValueData'

/**
 * 各学校预警分布
 */
export function useChartEarlyWarningDistributionEachSchool({
  numData,
}: {
  numData: Ref<NumData | null>
}) {
  const xAxisDataGrade = ref<string[]>([])

  const seriesGradeData = ref<any[]>([])

  const { levelDictValueData } = useLevelDictValueData({ numData })

  watchEffect(() => {
    if (!Array.isArray(numData.value?.allGradeLevelInterveneNum)) {
      return
    }

    if (!Array.isArray(levelDictValueData.value)) {
      return
    }

    xAxisDataGrade.value = []
    const gradeList: { [key in string]: Num[] } = {}

    numData.value?.allGradeLevelInterveneNum.map((item) => {
      if (!gradeList[item.schoolName]) {
        gradeList[item.schoolName] = []
        xAxisDataGrade.value.push(item.schoolName)
        levelDictValueData.value.map((item2, index) => {
          let data: any = {
            level: index + 1,
            num: 0,
          }
          gradeList[item.schoolName].push(data)
        })
      }
      gradeList[item.schoolName][item.level - 1] = item
    })

    const levelList: Level[][] = []

    // let newItem = { value: item.num, levelStr: item.level + "" };

    Object.values(gradeList).forEach((list) => {
      list.map((item) => {
        const level = item.level - 1
        if (!levelList[level]) {
          levelList[level] = []
        }
        levelList[level].push({ value: item.num, levelStr: item.level + '' })
      })
    })

    seriesGradeData.value = []
    let index = 0
    let colorList = [
      '#FF4500',
      '#FFA07A',
      '#FFD700',
      // '#224b8f',
      // "'#0D8FBF',",
      // '#7fb80e',
      '#8552a1',
      '#0D8FBF',
    ]
    levelList.map((item) => {
      seriesGradeData.value.push({
        name: levelDictValueData.value[index],
        type: 'bar',
        barMaxWidth: 20,
        emphasis: {
          focus: 'series',
        },
        data: item,
        itemStyle: {
          normal: {
            borderRadius: [5, 5, 0, 0],
            color: colorList[index],
            label: {
              show: true, //开启显示
              position: 'top', //在上方显示
              textStyle: {
                //数值样式
                // color: 'white',
                fontSize: 12,
              },
            },
          },
        },
      })
      index++
    })
  })

  return {
    xAxisDataGrade,
    seriesGradeData,
    levelDictValueData,
  }
}
